import 'dart:math';

import 'package:flutter/material.dart';

/// AnimatedContainer
/// 改变其属性有动画效果
/// 但是对child子控件无效
/// 主要用到了 random 函数, 生成随机数

void main() {
  runApp(MaterialApp(
    home: HomePage(),
  ));
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  double _width = 50;
  double _height = 50;
  Color _color = Colors.green;
  BorderRadiusGeometry _borderRadius = BorderRadius.circular(8);

  /// 随机改变宽度,高度,颜色和圆角
  void _randomArgs() {
    final random = Random();

    // Generate a random width and height.
    _width = random.nextInt(300).toDouble();
    _height = random.nextInt(300).toDouble();

    // Generate a random color.
    _color = Color.fromRGBO(
      random.nextInt(256),
      random.nextInt(256),
      random.nextInt(256),
      1,
    );
    // Generate a random border radius.
    _borderRadius = BorderRadius.circular(random.nextInt(100).toDouble());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('MyApp'),
      ),
      body: Center(
        child: AnimatedContainer(
          curve: Curves.ease,
          duration: Duration(seconds: 1),
          height: _height,
          width: _width,
          decoration: BoxDecoration(
            color: _color,
            borderRadius: _borderRadius,
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _randomArgs();
          });
        },
        child: Icon(Icons.add),
      ),
    );
  }
}
